package Model
{
	import View.shading.ShadingFromNeighbouringHouses;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class Shading extends EventDispatcher
	{
		private var _direction:String;
		private var _xAxis:Number = (0);	
		private var _yAxis:Number = 0;
		private var _alpha:Number = 0;
		private var _correctionFactor:Number = 1;
		private var _shadingFactor:Number = 1;
		private var _x1:Number;
		private var _y1:Number;
		private var _x2:Number;
		private var _y2:Number;
		private var _propertyChangedEvent:Event = new Event("changedProperty");
		
		
		public function Shading(direction:String){
			this._direction = direction;
		}
		
		private function CalculateAlpha():void
		{
			this._alpha = (Math.atan2(this._yAxis, this._xAxis))*(180/Math.PI);
		}

		public function CalculateShadingFactorForSummerShadingFacilities():void
		{
			this.shadingFactor = this.xAxis * this.correctionFactor;
		}
			
		public function CalculateShadingFactorForNeigbouringBuldingAndTrees():void{
					
			CalculateAlpha();
			
			if(this._alpha <= 10)
			{
				this._x1 = 0;
				this._y1 = 1;
				this._x2  = 10;
				this._y2 = 0.96;
			}
			else if(this._alpha <= 20 && this._alpha > 10)
			{
				this._x1 = 10;
				this._y1 = 0.96;
				this._x2  = 20;
				this._y2 = 0.82;
			}
			else if(this._alpha <= 30 && this._alpha > 20)
			{
				this._x1 = 20;
				this._y1 = 0.82;
				this._x2  = 30;
				this._y2 = 0.59;
			}
			else if( this._alpha <= 40 && this._alpha > 30)
			{
				this._x1 = 30;
				this._y1 = 0.59;
				this._x2  = 40;
				this._y2 = 0.45;
			}
			else if(this._alpha < 50 && this._alpha > 40)
			{
				this._x1 = 40;
				this._y1 = 0.45;
				this._x2  = 50;
				this._y2 = 0.35;
			}
			
			this.shadingFactor=(this._correctionFactor * (((this._y2 - this._y1)/(this._x2 - this._y2))*(this._alpha -this._x1) + this._y1));
		}
		
		public function CalculateShadingFactorForBalconies():void{
			
			CalculateAlpha();
			
			if(this._alpha <= 15)
			{
				this._x1 = 0;
				this._y1 = 1;
				this._x2  = 10;
				this._y2 = 0.95;
			}
			else if(this._alpha <= 30 && this._alpha > 15)
			{
				this._x1 = 10;
				this._y1 = 0.95;
				this._x2  = 20;
				this._y2 = 0.91;
			}
			else if(this._alpha <= 45 && this._alpha > 30)
			{
				this._x1 = 20;
				this._y1 = 0.91;
				this._x2  = 30;
				this._y2 = 0.75;
			}
			else if( this._alpha <= 60 && this._alpha > 45)
			{
				this._x1 = 30;
				this._y1 = 0.75;
				this._x2  = 40;
				this._y2 = 0.52;
			}
			else if(this._alpha < 75 && this._alpha > 60)
			{
				this._x1 = 40;
				this._y1 = 0.52;
				this._x2  = 50;
				this._y2 = 0.32;
			}
			
			this.shadingFactor=(this._correctionFactor * (((this._y2 - this._y1)/(this._x2 - this._y2))*(this._alpha -this._x1) + this._y1));
			
		}
		  
		public function SetDefaultProperties():void
		{
			this.alpha = 0;
			this.xAxis = 0;
			this.yAxis = 0;
			this.correctionFactor = 1;
			this.shadingFactor = 1;
		}
		
		public function get direction():String
		{
			return _direction;
		}

		public function set direction(value:String):void
		{
			_direction = value;
			
		}
		
		public function get xAxis():Number
		{
			return _xAxis;
		}

		public function set xAxis(value:Number):void
		{
			_xAxis = value;
		}

		public function get yAxis():Number
		{
			return _yAxis;
		}

		public function set yAxis(value:Number):void
		{
			_yAxis = value;
		}

		public function get alpha():Number
		{
			return _alpha;
		}

		public function set alpha(value:Number):void
		{
			_alpha = value;
		}

		public function get correctionFactor():Number
		{
			return _correctionFactor;
		}

		public function set correctionFactor(value:Number):void
		{
			_correctionFactor = value;
		}

		public function get shadingFactor():Number
		{
			return _shadingFactor;
		}

		public function set shadingFactor(value:Number):void
		{
			_shadingFactor = value;
			dispatchEvent(this._propertyChangedEvent);
		}


	}
}